




Type MapEditor_Module Extends Tab_Module

	

	Method Set()
	
		Local Gads : Widget[10]
		
		Local AssetW : Asset_Widget = New Asset_Widget
		Local AssAs : Asset = New Asset
		AssAs = AssAs.LoadAsset("media/editor/assetstrip.png")
		AssetW.SetIconAsset(AssAs)
		AssetW.SetAll("Asset Manager",0,0,ClientWidth(Panel)/4,ClientHeight(Panel),Panel)
		AssetW.Set()
		Gads[0] = Widget(AssetW)
		
		Local MapW : Mapeditor_Widget = New Mapeditor_Widget
		Local MapAs : Asset = New Asset
		MapAs = MapAs.LoadAsset("media/editor/toolbar.png")
		'MapW.
		MapW.SetIconAsset(MapAs)
		MapW.SetAll("Asset Manager",ClientWidth(Panel)/4,0,(ClientWidth(Panel)/4)*3,ClientHeight(Panel),Panel)
		MapW.Set()
		Gads[1] = Widget(MapW)
		
		Tab_Gadgets = Gads
		
	
	
	End Method
	
	

	



End Type


Type Mapeditor_Widget Extends Widget

	Field Current_TileSet : TileSet
	Field Current_Map : Map 
	Field FPS_Timer : TTimer
	Field FPS : Int = 30
	
	
	Method Update(EID:Int,Obj:Object,EEX:Object = Null)
	
		Select EID
			Case EVENT_TIMERTICK
				RedrawGadget Gadgets[5]
	
			Case EVENT_GADGETPAINT
			
				If Obj = Gadgets[5] Then
					SetGraphics CanvasGraphics(Gadgets[5])
					Cls
					SetBlend(ALPHABLEND)
					DrawText "Map",10,10
					Flip
				EndIf
			Case EVENT_MOUSEMOVE
				'Print "MOVE!"
				
	
			Case EVENT_GADGETACTION
			
				Select Obj
				
					Case Gadgets[3]
						
				
				End Select

		End Select
	
	
	End Method

	
	
	Method Set()
		Local Gads : TGadget[10]
		Panel = CreatePanel(Pos.x,Pos.y,Pos.w,Pos.h,Parent,PANEL_GROUP,Title)
		Gads[0] = CreateToolbar(IconAsset.FileBank,0,0,ClientWidth(Panel),30,Panel)
		'make some tools
		AddGadgetItem Gads[0], "", 0, GADGETICON_SEPARATOR	'Add a separator.
		AddGadgetItem Gads[0], "Draw", GADGETITEM_TOGGLE, 2, "This toggle button should change to a light bulb when clicked."
		Gads[1] = CreateLabel("Tileset: ",0,0,(ClientWidth(Panel)/2)/4,20,Panel,LABEL_CENTER)
		Gads[2] = CreateLabel("Map: ",((ClientWidth(Panel)/2)/4)*2,0,(ClientWidth(Panel)/2)/4,20,Panel,LABEL_CENTER)
		Gads[3] = CreateComboBox((ClientWidth(Panel)/2)/4,0,(ClientWidth(Panel)/2)/4,20,Panel)		
		
		For Local tset : Tileset = EachIn TilesetList
		
			If tset.AssetType = ASSET_TILESET Then
				AddGadgetItem(Gads[3],tset.ShortName,0,-1,"",tset)
			EndIf
		Next
		
		If CountGadgetItems(Gads[3]) > 0 Then SelectGadgetItem(Gads[3],0)
		
		Gads[4] = CreateComboBox((((ClientWidth(Panel)/2)/4)*2)+((ClientWidth(Panel)/2)/4),0,(ClientWidth(Panel)/2)/4,20,Panel)		
		Gads[5] = CreateCanvas( 0,30,ClientWidth(Panel)-30,ClientHeight(Panel)-60,Panel )
		Gads[6] = CreateSlider( (ClientWidth(Panel)-30),30,30,(ClientHeight(Panel)-60),Panel,SLIDER_VERTICAL )
		Gads[7] = CreateSlider(0,(ClientHeight(Panel)-30),(ClientWidth(Panel)-30),(ClientHeight(Panel)-60),Panel,SLIDER_HORIZONTAL )
		Gads[8] = CreateLabel("Layer: ",(((ClientWidth(Panel)/2)/4)*2)*2,0,(ClientWidth(Panel)/2)/4,20,Panel,LABEL_CENTER)
		Gads[9] = CreateComboBox(((((ClientWidth(Panel)/2)/4)*2)*2)+((ClientWidth(Panel)/2)/4),0,(ClientWidth(Panel)/2)/4,20,Panel)	
		Gadgets = Gads
		FPS_Timer = CreateTimer(FPS)
	End Method



End Type


Type Asset_Widget Extends Widget

	
	Field Asset_list : TList = New TList
	Field Tool_windows : TList = New TList
	

	Method Update(EID:Int,Obj:Object,EEX:Object = Null)
		
		Local EventAsset : Asset
		Local Asst : Asset
		Local TsetWindow : TileSetWindow
		Local MMapWindow : MapWindow
		Local Gnode : TGadget
		Select EID


			Case EVENT_GADGETACTION
				Select Obj
				
					Case Gadgets[0]
						DebugLog "clicked node "
						Gnode = SelectedTreeViewNode(Gadgets[0])
						If Gnode <> Null Then
							DebugLog "Have Gadget"
							EventAsset = Asset(GadgetExtra(Gnode))
							If EventAsset <> Null Then
								DebugLog "have asset"
								Select EventAsset.AssetType
								
									Case ASSET_TILESET
										TsetWindow = New TileSetWindow
										TsetWindow.AssetNode = Gnode
										TsetWindow.SetAll("Edit Tileset",0,0,300,300,Tool_windows)
										TsetWindow.TileAsset = Tileset(EventAsset)
										TsetWindow.EditMode = True
										TsetWindow.Set()										
										ListAddLast(Tool_windows,ToolWindow(TsetWindow))	
								End Select
							
							EndIf
						
						
							
						
						EndIf
				
					Case Gadgets[2]
						Select SelectedGadgetItem(Gadgets[4])
						
						
						Case ASSET_MAP
							'Local Asst : 
							MMapWindow = New MapWindow
							If SelectedTreeViewNode( Gadgets[0] ) = Null Then 
								MMapWindow.AssetNode = TreeViewRoot(Gadgets[0])
								MMapWindow.SetAll("New Map",0,0,300,300,Tool_windows)
								MMapWindow.Set()
								'TsetWindow.AssetNode = SelectedTreeViewNode( Gadgets[0] )							
								ListAddLast(Tool_windows,ToolWindow(MMapWindow))								
							Else
							
								Asst = Asset(GadgetExtra(SelectedTreeViewNode( Gadgets[0] )))
								If Asst = Null Then
									Notify "WAT DID YOU DO?!"
								Else
								
									If Asst.AssetType = 0 Then
										MMapWindow.SetAll("New Map",0,0,300,300,Tool_windows)
										MMapWindow.Set()
										MMapWindow.AssetNode = SelectedTreeViewNode( Gadgets[0] )							
										ListAddLast(Tool_windows,ToolWindow(MMapWindow))
									Else
										Notify "Please Choose a Folder"
									EndIf
								EndIf
							
							
							EndIf							
							
						
						Case ASSET_TILESET
							'Local Asst : Asset
							TsetWindow = New TileSetWindow

							If SelectedTreeViewNode( Gadgets[0] ) = Null Then 
								TsetWindow.AssetNode = TreeViewRoot(Gadgets[0])
								TsetWindow.SetAll("New Tileset",0,0,300,300,Tool_windows)
								TsetWindow.Set()
								'TsetWindow.AssetNode = SelectedTreeViewNode( Gadgets[0] )							
								ListAddLast(Tool_windows,ToolWindow(TsetWindow))								
							Else
							
								Asst = Asset(GadgetExtra(SelectedTreeViewNode( Gadgets[0] )))
								If Asst = Null Then
									Notify "WAT DID YOU DO?!"
								Else
								
									If Asst.AssetType = 0 Then
										TsetWindow.SetAll("New Tileset",0,0,300,300,Tool_windows)
										TsetWindow.Set()
										TsetWindow.AssetNode = SelectedTreeViewNode( Gadgets[0] )							
										ListAddLast(Tool_windows,ToolWindow(TsetWindow))
									Else
										Notify "Please Choose a Folder"
									EndIf
								EndIf
							
							
							EndIf

							
						End Select
				End Select
				

		End Select
	
		For Local TWin : ToolWindow = EachIn Tool_windows
		
			'TWin.Update(EID,OBJ)
			Select TWin.ToolType
			
				Case TOOL_TYPE_TILE
					Local TSetTool : TilesetWindow = TilesetWindow(TWin)
					TSetTool.Update(EID,OBJ)
				
				Case TOOL_TYPE_MAP
					Local MapTool : MapWindow = MapWindow(TWin)
					MapTool.Update(EID,OBJ)
								
			End Select
		Next
	
	End Method

	Method Add_Tileset()

		Local Tree : TGadget = SelectedTreeViewNode( Gadgets[2] )
		If Tree <> Null Then
		
			
		
		
		EndIf

	End Method

	
	Method Set()
	
		Local Gads : TGadget[5]
		Panel = CreatePanel(Pos.x,Pos.y,Pos.w,Pos.h,Parent,PANEL_GROUP,Title)
		Gads[0] = CreateTreeView(0,0,ClientWidth(Panel),ClientHeight(Panel)-50,Panel)
		If IconStrip <> Null Then
			SetGadgetIconStrip( Gads[0],IconStrip)
		EndIf
		LoadAssets(TreeViewRoot(Gads[0]))
		Rem
		
		Local FolderAsset : Asset = New Asset
		FolderAsset.ShortName = "Tilesets"
		Local TreeNode : TGadget = AddTreeViewNode( FolderAsset.ShortName,TreeViewRoot(Gads[0]),0,FolderAsset)
		ListAddLast(Asset_list,TreeNode)
		
		
		
		FolderAsset = New Asset
		FolderAsset.ShortName = "Maps"
		TreeNode = AddTreeViewNode( FolderAsset.ShortName,TreeViewRoot(Gads[0]),0,FolderAsset)
		ListAddLast(Asset_list,TreeNode)

		FolderAsset = New Asset
		FolderAsset.ShortName = "Objects"
		TreeNode = AddTreeViewNode( FolderAsset.ShortName,TreeViewRoot(Gads[0]),0,FolderAsset)
		ListAddLast(Asset_list,TreeNode)
		
		FolderAsset = New Asset
		FolderAsset.ShortName = "Sprites"
		TreeNode = AddTreeViewNode( FolderAsset.ShortName,TreeViewRoot(Gads[0]),0,FolderAsset)
		ListAddLast(Asset_list,TreeNode)
		
		FolderAsset = New Asset
		FolderAsset.ShortName = "Sounds"
		TreeNode = AddTreeViewNode( FolderAsset.ShortName,TreeViewRoot(Gads[0]),0,FolderAsset)
		ListAddLast(Asset_list,TreeNode)	

		FolderAsset = New Asset
		FolderAsset.ShortName = "Music"
		TreeNode = AddTreeViewNode( FolderAsset.ShortName,TreeViewRoot(Gads[0]),0,FolderAsset)
		ListAddLast(Asset_list,TreeNode)			
		
		FolderAsset = New Asset
		FolderAsset.ShortName = "Images"
		TreeNode = AddTreeViewNode( FolderAsset.ShortName,TreeViewRoot(Gads[0]),0,FolderAsset)
		ListAddLast(Asset_list,TreeNode)	
		
		FolderAsset = New Asset
		FolderAsset.ShortName = "Scripts"
		TreeNode = AddTreeViewNode( FolderAsset.ShortName,TreeViewRoot(Gads[0]),0,FolderAsset)
		ListAddLast(Asset_list,TreeNode)			
		
		End Rem
		
		Gads[1] = CreateButton("Add Folder",0,ClientHeight(Panel)-50,ClientWidth(Panel)/2,20,Panel,BUTTON_PUSH)
		Gads[2] = CreateButton("Add Asset",ClientWidth(Panel)/2,ClientHeight(Panel)-50,ClientWidth(Panel)/2,20,Panel,BUTTON_PUSH)
		Gads[3] = CreateButton("Remove",0,ClientHeight(Panel)-30,ClientWidth(Panel)/2,20,Panel,BUTTON_PUSH)
		Gads[4] = CreateComboBox(ClientWidth(Panel)/2,ClientHeight(Panel)-30,ClientWidth(Panel)/2,20,Panel)
		AddGadgetItem(Gads[4],"Blank",0,-1,"")
		AddGadgetItem(Gads[4],"Image",0,-1,"")
		AddGadgetItem(Gads[4],"Script",0,-1,"")
		AddGadgetItem(Gads[4],"Tileset",0,-1,"")
		AddGadgetItem(Gads[4],"Sprite",0,-1,"")
		AddGadgetItem(Gads[4],"Map",0,-1,"")
		AddGadgetItem(Gads[4],"Sound",0,-1,"")
		AddGadgetItem(Gads[4],"Music",0,-1,"")
		AddGadgetItem(Gads[4],"Object",0,-1,"")						
		SelectGadgetItem(Gads[4],0)
		Gadgets = Gads
	End Method
	
	Method LoadAssets(Parent : TGadget, Path : String = "")
		Local TreeNode : TGadget
		Local FileAsset : Asset
		Local T : String
		If Path = "" Then 
			Path = "Projects/"+CurrentProject
		EndIf
		Local dir : Int = ReadDir( Path )	
		Repeat
			T=NextFile( dir )
			If T="" Exit
			If T="." Or T=".." Or T=".svn" Continue
			
			Select FileType(Path+"/"+T)	
			
			
				Case 1
					Select Lower(ExtractExt(Path+"/"+T))
					
						Case "tset"
							FileAsset = New Tileset
							DebugLog "Adding Tileset: "+Replace(Path,"Projects/"+CurrentProject,"")
							FileAsset = FileAsset.LoadAsset(Path+"/"+T,Replace(Path,"Projects/"+CurrentProject,""))
								
							If FileAsset = Null
								Notify "Could not Load "+Replace(Path+"/"+T,"Projects/"+CurrentProject,"")
								Continue
							Else
							
								
								FileAsset.AssetParent = Asset(GadgetExtra(Parent))
								TreeNode = AddTreeViewNode( Replace(FileAsset.ShortName,"."+ExtractExt(FileAsset.ShortName),""),Parent,FileAsset.AssetType,FileAsset)
								ListAddLast(Asset_list,TreeNode)						
								LoadAssets(TreeNode,Path+"/"+T)							
								
								
							EndIf
							
							
							
						Case "map"
							FileAsset = New Map
							DebugLog "Adding Map: "+Replace(Path,"Projects/"+CurrentProject,"")
							FileAsset = FileAsset.LoadAsset(Path+"/"+T,Replace(Path,"Projects/"+CurrentProject,""))
							If FileAsset = Null
								Notify "Could not Load "+Replace(Path+"/"+T,"Projects/"+CurrentProject,"")
								Continue
							Else
							
								
								FileAsset.AssetParent = Asset(GadgetExtra(Parent))
								TreeNode = AddTreeViewNode( Replace(FileAsset.ShortName,"."+ExtractExt(FileAsset.ShortName),""),Parent,FileAsset.AssetType,FileAsset)
								ListAddLast(Asset_list,TreeNode)						
								LoadAssets(TreeNode,Path+"/"+T)							
								
								
							EndIf							
					End Select
				Case 2
					Local FolderAsset : Asset = New Asset
					FolderAsset.ShortName = T
					FolderAsset.AssetPath = Replace(Path+"/"+T,"Projects/"+CurrentProject,"")
					TreeNode = AddTreeViewNode( FolderAsset.ShortName,Parent,0,FolderAsset)
					ListAddLast(Asset_list,TreeNode)						
					LoadAssets(TreeNode,Path+"/"+T)
			
			
			End Select
		Forever
		Return	
	
		
	
	End Method
	
	
End Type